package com.amigo.online.provider.manager.config;

import java.net.UnknownHostException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import com.mongodb.MongoClientURI;

@Configuration
public class MongoConfig {

    @Value("${spring.data.mongodb.user.uri}")
    private String mongoUserUri;

    @Value("${spring.data.mongodb.merchant.uri}")
    private String mongoMerchantUri;

    @Bean
    public MongoMappingContext mongoMappingContext() {
        MongoMappingContext mappingContext = new MongoMappingContext();
        return mappingContext;
    }

    @Bean
    @Primary
    public MongoDbFactory dbUserFactory() throws UnknownHostException {
        return new SimpleMongoDbFactory(new MongoClientURI(mongoUserUri));
    }

    @Bean("mongoTemplate")
    @Primary
    public MongoTemplate userMongoTemplate() throws Exception {
        return new MongoTemplate(this.dbUserFactory());
    }

    @Bean
    public MongoDbFactory dbMerchantFactory() throws UnknownHostException {
        return new SimpleMongoDbFactory(new MongoClientURI(mongoMerchantUri));
    }

    @Bean("merchantMongoTemplate")
    public MongoTemplate readMerchantMongoTemplate() throws Exception {
        return new MongoTemplate(this.dbMerchantFactory());
    }


}
